﻿using JetBrains.Annotations;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.ExtensionsAPI.Resolve;
using JetBrains.ReSharper.Psi.Resolve;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Xaml.Impl.Util;
using JetBrains.ReSharper.Psi.Xaml.Tree;

namespace GammaJul.ReSharper.GammaXaml.Psi.Xaml {
	
	/// <summary>
	/// A <see cref="IQualifier"/> that represents the current data context (or design data context) of a XAML node.
	/// </summary>
	internal sealed class DataContextQualifier : IQualifier {
		private readonly ITreeNode _node;

		[NotNull]
		internal IType GetExpressionType() {
			IXamlObjectElement element;
			return BindingUtil.CalculateSourceType(_node, out element);
		}

		public ISymbolTable GetSymbolTable(SymbolTableMode mode) {
			IType type = GetExpressionType();
			return type.IsUnknown || mode == SymbolTableMode.TYPE_AND_NAMESPACES
				? EmptySymbolTable.INSTANCE
				: type.GetSymbolTable(_node.GetPsiModule());
		}

		public QualifierKind GetKind() {
			return QualifierKind.OBJECT;
		}

		public bool Resolved {
			get { return !GetExpressionType().IsUnknown; }
		}

		public DataContextQualifier(ITreeNode node) {
			_node = node;
		}

	}

}